package com.scut.edu.wanyou.home.talk;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import android.util.Log;

import com.scut.edu.wanyou.Constans;
import org.jivesoftware.smack.packet.Presence;

/**
 * @author Sam.Io
 * @time 2011/11/18
 * @project AdXmpp
 */
public class XmppTool {

	private static XMPPConnection con = null;
	public static void openConnection() {
		try {
			ConnectionConfiguration connConfig = new ConnectionConfiguration(Constans.SERVER_IP,Constans.TALK_PORT);
			connConfig.setSASLAuthenticationEnabled(false);
			con = new XMPPConnection(connConfig);
			con.connect();
		} catch (XMPPException xe) {
			Log.e("XmppTool.class","连接服务器出错");
			xe.printStackTrace();
		}
	}

	public static XMPPConnection getConnection() {
		if (con == null ) {
			openConnection();
		}
		return con;
	}

	public static void closeConnection() {
        Presence presence = new Presence(Presence.Type.unavailable);
        con.sendPacket(presence);
		con.disconnect();
		con = null;
	}
	
}
